package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.enumPackage.Constant;
import com.wuwei.elearning.question.domain.Collect;
import com.wuwei.elearning.question.domain.Subject;
import com.wuwei.elearning.question.domain.WrongRecord;
import com.wuwei.elearning.question.mapper.CollectMapper;
import com.wuwei.elearning.question.mapper.SubjectMapper;
import com.wuwei.elearning.question.mapper.WrongRecordMapper;
import com.wuwei.elearning.question.vo.EChartsBarVo;
import com.wuwei.elearning.question.vo.EChartsPieVo;
import com.wuwei.elearning.utils.result.Result;
import com.wuwei.elearning.utils.token.TokenUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-04-01
 */
@RestController
@RequestMapping("/elearning/wrongRecord")
@CrossOrigin
public class WrongRecordController {


    @Resource
    private WrongRecordMapper wrongRecordMapper;

    @Resource
    private SubjectMapper subjectMapper;

    @Autowired
    private CollectMapper collectMapper;


    @GetMapping("wrongAndCollect/all")
    public Result<EChartsBarVo> wrongAndCollect(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        List<Long> rootSubjectIds = Arrays.asList(Constant.SUBJECTIDS);
        return getEChartsBarVoResult(account,rootSubjectIds);
    }

    @GetMapping("wrongAndCollect/bySubject/{subjectId}")
    public Result<EChartsBarVo> wrongAndCollectBySubject(HttpServletRequest request,@PathVariable("subjectId") Long subjectId) {
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        List<Long> rootSubjectIds = subjectMapper.getLeafsIdByParentId(subjectId);
        return getEChartsBarVoResult(account,rootSubjectIds);
    }

    @NotNull
    private Result<EChartsBarVo> getEChartsBarVoResult(Long account, List<Long> subjectIds) {
        EChartsBarVo eChartsBarVo = new EChartsBarVo();
        List<String> collectData = eChartsBarVo.getData();
        List<String> wrongData = eChartsBarVo.getData2();
        List<String> xAxis = eChartsBarVo.getXAxis();
        List<Long> ids = eChartsBarVo.getIds();
        for (Long subjectId : subjectIds) {
            List<Subject> subjects = subjectMapper.getLeafsById(subjectId);
            List<Long> subjectIdList = subjects.stream().map(Subject::getId).collect(Collectors.toList());
            subjectIdList.add(subjectId);
            Long collectCount = collectMapper.selectCount(new LambdaQueryWrapper<Collect>()
                    .in(Collect::getSubjectId, subjectIdList)
                    .eq(Collect::getUserAccount, account)
            );
            Long wrongCount = wrongRecordMapper.selectCount(new LambdaQueryWrapper<WrongRecord>()
                    .eq(WrongRecord::getUserAccount, account)
                    .in(WrongRecord::getSelectedId, subjectIdList));
            if (collectCount == 0&&wrongCount == 0) {
                continue;
            }
            collectData.add(String.valueOf(collectCount));
            wrongData.add(String.valueOf(wrongCount));
            xAxis.add(subjectMapper.selectById(subjectId).getName());
            ids.add(subjectId);
        }
        return Result.ok(eChartsBarVo);
    }

    /**
     * 增加错题
     * @param request
     * @return
     */
    @PostMapping("addWrongRecord/{questionId}/{selectedId}")
    public Result<String> addWrongRecord(@PathVariable("questionId")Long questionId,@PathVariable("selectedId") Long selectedId,
                                      HttpServletRequest request){
        if (questionId.equals(-1L)) {
            return Result.ok("不需要更新错题！");
        }
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        WrongRecord wrongRecord = new WrongRecord();
        wrongRecord.setCreatedTime(LocalDateTime.now());
        wrongRecord.setSelectedId(selectedId);
        wrongRecord.setQuestionId(questionId);
        wrongRecord.setUserAccount(account);
        wrongRecordMapper.insert(wrongRecord);
        return Result.ok("错题已收集");
    }

    @DeleteMapping("deleteByQuestionId/{questionId}")
    public Result<String> deleteByQuestionId(@PathVariable("questionId") Long questionId, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        LambdaQueryWrapper<WrongRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WrongRecord::getQuestionId,questionId).eq(WrongRecord::getUserAccount,account);
        wrongRecordMapper.delete(queryWrapper);
        return Result.ok("错题删除成功！");
    }

}

